<template>
  <div class="upload-container">
    <el-button :style="{ background: props.color, borderColor: props.color }" @click="dataObj.dialogVisible = true" type="primary"
      >上传图片 <el-icon><Upload /></el-icon
    ></el-button>
    <el-dialog append-to-body v-model="dataObj.dialogVisible" title="上传图片" width="30%" center align-center>
      <div style="display: flex; justify-content: center; align-items: center">
        <el-upload ref="upload_div" class="editor-slide-upload" name="file" :action="minioUploadUrl" :multiple="true" :file-list="fileList" :show-file-list="true" list-type="picture-card" :on-remove="handleRemove" :on-success="handleSuccess" :before-upload="beforeUpload">
          <el-button type="primary" size="default">点击上传</el-button>
        </el-upload>
      </div>
      <div style="display: flex; justify-content: center; align-items: center">
        <el-button size="default" @click="dataObj.dialogVisible = false">取 消</el-button>
        <el-button size="default" type="primary" @click="handleSubmit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script setup>
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus'
import { Upload } from '@element-plus/icons-vue'
const props = defineProps({
  color: {
    type: String,
    default: '#1890ff'
  }
})

let dataObj = reactive({
  policy: '',
  signature: '',
  key: '',
  ossaccessKeyId: '',
  dir: '',
  host: '',
  dialogVisible: false
  // callback:'',
})

const minioUploadUrl = ref('http://localhost:8080/minio/upload')
let listObj = reactive({})
let fileList = reactive([])
const useOss = ref(false)

const emits = defineEmits(['successCBK'])

function checkAllSuccess() {
  return Object.keys(listObj).every(item => listObj[item].hasSuccess)
}

function handleSubmit() {
  const arr = Object.keys(listObj).map(v => listObj[v])
  if (!checkAllSuccess()) {
    ElMessage('请等待所有图片上传成功 或 出现了网络问题，请刷新页面重新上传！')
    return
  }
  emits('successCBK', arr)
  listObj = {}
  fileList = []
  dataObj.dialogVisible = false
}
function handleSuccess(response, file) {
  const uid = file.uid
  const objKeyArr = Object.keys(listObj)
  for (let i = 0, len = objKeyArr.length; i < len; i++) {
    if (listObj[objKeyArr[i]].uid === uid) {
      listObj[objKeyArr[i]].url = dataObj.host + '/' + dataObj.dir + '/' + file.name
      if (!useOss.value) {
        //不使用oss直接获取图片路径
        listObj[objKeyArr[i]].url = response.data.url
      }
      listObj[objKeyArr[i]].hasSuccess = true
      return
    }
  }
}
function handleRemove(file) {
  const uid = file.uid
  const objKeyArr = Object.keys(listObj)
  for (let i = 0, len = objKeyArr.length; i < len; i++) {
    if (listObj[objKeyArr[i]].uid === uid) {
      delete listObj[objKeyArr[i]]
      return true
    }
  }
  return false
}
function beforeUpload(file) {
  const fileName = file.uid
  listObj[fileName] = {}
  if (!useOss.value && listObj != null) {
    //不使用oss不需要获取策略
    listObj[fileName] = { hasSuccess: false, uid: file.uid, width: 720, height: 360 }
    return true
  }
  return false
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.upload-container .editor-slide-upload {
  margin-bottom: 20px;
}
</style>
